package com.exm.collectcodenew.timer;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
// ScheduledThreadPoolExecutor 的其他任务不受抛出异常的任务的影响，是因为在 ScheduledThreadPoolExecutor 中的ScheduledFutureTask 任务中 catch 掉了异常，
// 但是在线程池任务的 run 方法内使用catch 捕获异常并打印日志是最佳实践。
public class TestScheduledThreadPoolExcutor {
    static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor =new ScheduledThreadPoolExecutor(1);

    public static void main(String[] args) {
        scheduledThreadPoolExecutor.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("---one Task-----");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                }
                throw new RuntimeException("error");
            }
        },500, TimeUnit.MICROSECONDS);
        scheduledThreadPoolExecutor.schedule(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    System.out.println("---two Task---");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        },1000,TimeUnit.MICROSECONDS);
        scheduledThreadPoolExecutor.shutdown();
    }
}
